package org.jeecg.modules.outbound.service.impl;

import cn.hutool.core.util.IdUtil;
import liquibase.pro.packaged.L;
import org.jeecg.modules.inventory.entity.BussInventory;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.entity.BussInventoryInoutDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryInoutDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryMapper;
import org.jeecg.modules.outbound.entity.BussOutboundOrderDetail;
import org.jeecg.modules.outbound.entity.BussOutboundOrderManagement;
import org.jeecg.modules.outbound.entity.BussOutboundOrderInformation;
import org.jeecg.modules.outbound.mapper.BussOutboundOrderDetailMapper;
import org.jeecg.modules.outbound.mapper.BussOutboundOrderInformationMapper;
import org.jeecg.modules.outbound.mapper.BussOutboundOrderManagementMapper;
import org.jeecg.modules.outbound.service.IBussOutboundOrderManagementService;
import org.jeecg.modules.outbound.vo.BussOutboundOrderDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 出库单管理
 * @Author: jeecg-boot
 * @Date:   2024-06-18
 * @Version: V1.0
 */
@Service
public class BussOutboundOrderManagementServiceImpl extends ServiceImpl<BussOutboundOrderManagementMapper, BussOutboundOrderManagement> implements IBussOutboundOrderManagementService {

	@Autowired
	private BussOutboundOrderManagementMapper bussOutboundOrderManagementMapper;
	@Autowired
	private BussOutboundOrderInformationMapper bussOutboundOrderInformationMapper;
	@Autowired
	private BussOutboundOrderDetailMapper bussOutboundOrderDetailMapper;
	@Autowired
	private BussInventoryMapper bussInventoryMapper;
	@Autowired
	private BussInventoryDetailsMapper bussInventoryDetailsMapper;
	@Autowired
	private BussInventoryInoutDetailsMapper bussInventoryInoutDetailsMapper;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussOutboundOrderManagement bussOutboundOrderManagement, List<BussOutboundOrderInformation> bussOutboundOrderInformationList,List<BussOutboundOrderDetailVo> outboundOrderDetailList) {
		bussOutboundOrderManagement.setStatus("1");//默认已审批
		bussOutboundOrderManagementMapper.insert(bussOutboundOrderManagement);
		if(bussOutboundOrderInformationList!=null && bussOutboundOrderInformationList.size()>0) {
			for(BussOutboundOrderInformation entity:bussOutboundOrderInformationList) {
				entity.setId(null);
				//外键设置
				entity.setOutboundId(bussOutboundOrderManagement.getId());
			}
			bussOutboundOrderInformationMapper.insertBatchSomeColumn(bussOutboundOrderInformationList);
		}
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			List<BussOutboundOrderDetail> orderDetails=new ArrayList<>();
			for(BussOutboundOrderDetailVo entity:outboundOrderDetailList) {
				BussOutboundOrderDetail bussOutboundOrderDetail = new BussOutboundOrderDetail();
				BeanUtils.copyProperties(entity, bussOutboundOrderDetail);
				entity.setId(null);
				//外键设置
				entity.setInformationId(bussOutboundOrderManagement.getId());
				if (entity.getQuantity() != null && entity.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
					orderDetails.add(entity);
				}
			}
			if(orderDetails.size()>0){
				bussOutboundOrderDetailMapper.insertBatchSomeColumn(orderDetails);
			}
		}
		saveInventory(bussOutboundOrderManagement,bussOutboundOrderInformationList,outboundOrderDetailList);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussOutboundOrderManagement bussOutboundOrderManagement,List<BussOutboundOrderInformation> bussOutboundOrderInformationList,List<BussOutboundOrderDetailVo> outboundOrderDetailList) {
		//判断出库明细对应的库存存不存在，不存在提示
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			for(BussOutboundOrderDetailVo detailVo:outboundOrderDetailList) {
				BussInventoryDetails details = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
				if (details == null){
					throw new RuntimeException("出库单对应的库存明细发生变化，请将出库单信息内容删除重新选择出库的物料！");
				}
			}
		}
		bussOutboundOrderManagement.setStatus("1");//默认已审批
		bussOutboundOrderManagementMapper.updateById(bussOutboundOrderManagement);
		
		//1.先删除子表数据
		bussOutboundOrderInformationMapper.deleteByMainId(bussOutboundOrderManagement.getId());
		bussOutboundOrderDetailMapper.deleteByMainId(bussOutboundOrderManagement.getId());
		
		//2.子表数据重新插入
		if(bussOutboundOrderInformationList!=null && bussOutboundOrderInformationList.size()>0) {
			for(BussOutboundOrderInformation entity:bussOutboundOrderInformationList) {
				entity.setId(null);
				//外键设置
				entity.setOutboundId(bussOutboundOrderManagement.getId());
				bussOutboundOrderInformationMapper.insert(entity);
			}
		}
		if(outboundOrderDetailList!=null && outboundOrderDetailList.size()>0) {
			for(BussOutboundOrderDetailVo entity:outboundOrderDetailList) {
				BussOutboundOrderDetail bussOutboundOrderDetail = new BussOutboundOrderDetail();
				BeanUtils.copyProperties(entity, bussOutboundOrderDetail);
				entity.setId(null);
				//外键设置
				entity.setInformationId(bussOutboundOrderManagement.getId());
				if (entity.getQuantity() != null && entity.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
					bussOutboundOrderDetailMapper.insert(entity);
				}
//				if (entity.getChildren()!=null && entity.getChildren().size()>0){
//					for(BussOutboundOrderDetailVo temp:entity.getChildren()) {
//						BussOutboundOrderDetail detail = new BussOutboundOrderDetail();
//						BeanUtils.copyProperties(temp, detail);
//						detail.setId(null);
//						detail.setInformationId(bussOutboundOrderManagement.getId());
//						detail.setPid(entity.getId());
//						detail.setHasChild("0");
//						bussOutboundOrderDetailMapper.insert(detail);
//					}
//				}
			}
		}
		saveInventory(bussOutboundOrderManagement,bussOutboundOrderInformationList,outboundOrderDetailList);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
//		BussOutboundOrderManagement management = bussOutboundOrderManagementMapper.selectById(id);
//		if(management.getStatus().equals("1")) {
//			//删除已审核的数据 先调用库存恢复
			Collection<String> stringCollection = new ArrayList<>();
			stringCollection.add(id);
			//回退数量并删除出库单
			this.revokeBatch(stringCollection);
//		}
//		bussOutboundOrderInformationMapper.deleteByMainId(id);
//		bussOutboundOrderDetailMapper.deleteByMainId(id);
//		bussOutboundOrderManagementMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		//回退数量并删除出库单
		this.revokeBatch(idList);
//		for(Serializable id:idList) {
//			BussOutboundOrderManagement management = bussOutboundOrderManagementMapper.selectById(id);
//			if(management.getStatus().equals("1")) {
//				//删除已审核的数据 先调用库存恢复
//				Collection<String> stringCollection = new ArrayList<>();
//				stringCollection.add(id.toString());
//				//修改状态
//				this.revokeBatch(stringCollection);
//			}
//			bussOutboundOrderInformationMapper.deleteByMainId(id.toString());
//			bussOutboundOrderDetailMapper.deleteByMainId(id.toString());
//			bussOutboundOrderManagementMapper.deleteById(id);
//		}
	}

	@Override
	public BussOutboundOrderManagement selectForPrint(String id) {
		return bussOutboundOrderManagementMapper.selectForPrint(id);
	}

	/**
	 *
	 *
	 *
	 * @param bussOutboundOrderManagement
	 * @param bussOutboundOrderInformationList
	 * @param outboundOrderDetailList
	 */
	public void saveInventory(BussOutboundOrderManagement bussOutboundOrderManagement,List<BussOutboundOrderInformation> bussOutboundOrderInformationList,List<BussOutboundOrderDetailVo> outboundOrderDetailList){

		for (BussOutboundOrderDetailVo detailVo:outboundOrderDetailList){
			if(detailVo.getQuantity() != null && detailVo.getQuantity().compareTo(BigDecimal.ZERO) != 0) {
				//库存明细出库数量，库存数量
				BussInventoryDetails details = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
				details.setOutboundQuantity(details.getOutboundQuantity().add(detailVo.getQuantity()));
				details.setQuantity(details.getQuantity().subtract(detailVo.getQuantity()));
				bussInventoryDetailsMapper.updateById(details);
				//生成库存明细出库记录
				BussInventoryInoutDetails bussInventoryInoutDetails = new BussInventoryInoutDetails();
				bussInventoryInoutDetails.setOperator(bussOutboundOrderManagement.getCreateBy());
				bussInventoryInoutDetails.setOperationTime(bussOutboundOrderManagement.getCreateTime());
				bussInventoryInoutDetails.setBusiness("5");//业务默认出库
				bussInventoryInoutDetails.setBusinessType("7");//业务类型判断暂时赋值原料出库
				bussInventoryInoutDetails.setQuantity(detailVo.getQuantity());
				bussInventoryInoutDetails.setBusinessCode(bussOutboundOrderManagement.getCode());
				bussInventoryInoutDetails.setBusinessId(bussOutboundOrderManagement.getId());
				bussInventoryInoutDetails.setInventoryId(details.getInventoryId());
				bussInventoryInoutDetails.setDetailId(details.getId());
				bussInventoryInoutDetails.setRemark(detailVo.getRemark());
				bussInventoryInoutDetailsMapper.insert(bussInventoryInoutDetails);
			}
		}
	}

	@Override
	public void revokeBatch(Collection<? extends Serializable> idList) {
		for (Serializable id : idList) {
//			BussOutboundOrderManagement management = bussOutboundOrderManagementMapper.selectById(id.toString());
//			management.setStatus("0");
//			bussOutboundOrderManagementMapper.updateById(management);
			//修改库存数量
			quantityRefund(id.toString());
			//todo 如果是生产领料撤回修改领料单的状态

			// 撤回后删除出库单
			bussOutboundOrderInformationMapper.deleteByMainId(id.toString());
			bussOutboundOrderDetailMapper.deleteByMainId(id.toString());
			bussOutboundOrderManagementMapper.deleteById(id);
		}
	}

	public void quantityRefund(String id){
		BussOutboundOrderManagement management = bussOutboundOrderManagementMapper.selectById(id);
		//查询出库明细
		List<BussOutboundOrderDetail> details = bussOutboundOrderDetailMapper.selectByInfoId(id);
		for (BussOutboundOrderDetail detailVo : details){
			//todo 按库存id查询，如果查询不到，找同一入库单同一种货有库存数量的记录
			//将数量加回库存明细
			BussInventoryDetails inventory = bussInventoryDetailsMapper.selectById(detailVo.getInventoryId());
			if(inventory == null){
				//查询同一入库单同一种货的库存明细，按数量倒叙取第一条
				inventory = bussInventoryDetailsMapper.selectByStorageInfo(detailVo.getWarehousingId(),detailVo.getMaterialId());
			}
			inventory.setQuantity(inventory.getQuantity().add(detailVo.getQuantity()));
			//可用数量
			inventory.setOutboundQuantity(inventory.getOutboundQuantity().subtract(detailVo.getQuantity()));
			bussInventoryDetailsMapper.updateById(inventory);
		}
		//删除对应的库存出库明细
		bussInventoryInoutDetailsMapper.deleteByBusinessId(id,management.getCode());

	}
}
